$(function () {
    var studentTracking_datagrid = $("#studentTracking_datagrid");
    var studentTracking_dialog = $("#studentTracking_dialog");
    var studentTracking_form = $("#studentTracking_form");
    studentTracking_datagrid.datagrid({
        url: '/studentTracking/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'date', title: '日期', width: 100,},
            {field: 'client', title: '客户', width: 100,formatter:function (value) {
                return value ? value.name : "";
            }},
            {field: 'consultant', title: '咨询人员', width: 100},
            {field: 'nextTime', title: '下次访问时间', width: 100,formatter:function (value,row) {
                var value = row.client;
                console.log(value);
                return value ?value.nextTime : "";
            }},
            {field: 'followPurpose', title: '跟进目的', width: 100},
            {field: 'dictionaryItem', title: '意向程度', width: 100,formatter:function (value,row) {
                var value = row.client;
                return value? value.intention.name: "";
            }},
            {field: 'qq', title: 'QQ', width: 100,formatter:function (value,row) {
                var value = row.client;
                return value ? value.qq : "";
            }},
            {field: 'tel', title: '联系电话', width: 100,formatter:function (value,row) {
                var value = row.client;
                return value ? value.tel : "";
            }},
            {field: 'dictionaryItem.name', title: '咨询方式', width: 100,formatter:function (value,row) {
                console.log(row);
                var value = row.dictionaryItem;
                return value ? value.name : "";
            }},
    {
                field: 'auditStatus', title: '审核状态', width: 100, formatter: function (value) {
                return value ? "<font color='green'>已审核</font>" : "<font color='red'>未审核</font>"
            }
            },
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#studentTracking_btns",
        onClickRow: function (index, row) {
            if (row.auditStatus) {
                $('#changeState_btn').hide();
            } else {
                $('#changeState_btn').show();
                $("#changeState_btn").linkbutton({
                    text:'审核'
                })
            }
        }
    });
    studentTracking_dialog.dialog({
        width: 730,
        height: 400,
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            studentTracking_form.form('clear');
        }
    });
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObject[methodName]();
    })
    var methodObject = {
        /*add: function () {
         studentTracking_dialog.dialog('setTitle', '新增员工');
         studentTracking_dialog.dialog('open');
         },*/

        edit: function () {
            var row = studentTracking_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            console.log(row);
            row['id'] = row.id;
            row['client.id'] = row.client.id;
            row['school.id'] = row.school.id;
            row['schoolLinkman.id'] = row.schoolLinkman.id;
            row['classRoom.id'] = row.classRoom.id;
            row['dictionaryItem.id'] = row.dictionaryItem.id;


            row['client.name'] = row.client.name;
            row['client.qq'] = row.client.qq;
            row['client.tel'] = row.client.tel;
            row['client.nextTime'] = row.client.nextTime;
            row['client.postionTime'] = row.client.postionTime;
            row['client.question'] = row.client.question;
            row['client.intention.id'] = row.client.intention.id;
            row['client.schoolName'] = row.client.schoolName;
            row['client.intentionClass.id'] = row.client.intentionClass.id;
            row['client.source'] = row.client.source;
            row['classRoom.className'] = row.classRoom.className;
            row['schoolLinkman.name'] = row.schoolLinkman.name;
            row['linkman'] = row.linkman;
            studentTracking_form.form('load', row
            );
            studentTracking_dialog.dialog('setTitle', '客户跟踪管理');
            studentTracking_dialog.dialog('open');
            $("#studentTracking_form input").prop("disabled",false);
        },
        view: function () {
            var row = studentTracking_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            row['id'] = row.id;
            row['client.id'] = row.client.id;
            row['school.id'] = row.school.id;
            row['schoolLinkman.id'] = row.schoolLinkman.id;
            row['classRoom.id'] = row.classRoom.id;
            row['dictionaryItem.id'] = row.dictionaryItem.id;


            row['client.name'] = row.client.name;
            row['client.qq'] = row.client.qq;
            row['client.tel'] = row.client.tel;
            row['client.nextTime'] = row.client.nextTime;
            row['client.postionTime'] = row.client.postionTime;
            row['client.question'] = row.client.question;
            row['client.intention.id'] = row.client.intention.id;
            row['client.schoolName'] = row.client.schoolName;
            row['client.intentionClass.id'] = row.client.intentionClass.id;
            row['client.source'] = row.client.source;
            row['classRoom.className'] = row.classRoom.className;
            row['schoolLinkman.name'] = row.schoolLinkman.name;
            row['linkman'] = row.linkman;
            studentTracking_form.form('load', row
            );
            studentTracking_dialog.dialog('setTitle', '客户跟踪查看');
            studentTracking_dialog.dialog('open');
            $("#studentTracking_form input").prop("disabled",true);
        },
        changeState: function () {
            var row = studentTracking_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/studentTracking/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            studentTracking_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            studentTracking_datagrid.datagrid('reload');
        },

        save: function () {
            studentTracking_form.form('submit', {
                url: '/studentTracking/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObject.cancel();
                        studentTracking_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            studentTracking_dialog.dialog('close');
        },

        cancel2: function () {
            $("#reset_password").dialog('close');
        },

        query: function () {
            var keyword = $("[name=keyword]").val();
            studentTracking_datagrid.datagrid('load', {
                keyword: keyword
            })
        },

        resetPassword: function () {
            $("#password_form").form('submit', {
                url: '/studentTracking/resetPassword.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '操作成功', 'info');
                        methodObject.cancel2();
                        studentTracking_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },
        import_save: function () {
            $("#import_form").form('submit', {
                url: '/studentTracking/importXls.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '上传成功', 'info');
                        methodObject.import_cancel();
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },
        import_cancel: function () {
            $("#import_dialog").dialog('close');
        }
    }


    $("#schoolName").combobox({
        onChange:function (newValue,oldValue) {
            $.post("/linkman/selectBySchoolName.do",{schoolName:newValue},function (data) {
                $("#schoolLinkman").combobox({
                    data:data,
                    panelHeight:true,
                    valueField:'name',
                    textField:'name'
                });

            })
        }
    })
})

